package com.moyq5.mvc.maker.test.feign.server.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.moyq5.mvc.framework.general.Body;
import com.moyq5.mvc.framework.general.Page;
import com.moyq5.mvc.framework.general.Paging;
import com.moyq5.mvc.framework.general.Sorting;
import com.moyq5.mvc.framework.jackson.result.ListResult;
import com.moyq5.mvc.framework.jackson.result.ObjectResult;
import com.moyq5.mvc.framework.jackson.result.PageResult;
import com.moyq5.mvc.framework.jackson.result.StatusResult;
import com.moyq5.mvc.framework.utils.EntityUtils;
import com.moyq5.mvc.maker.test.bean.entity.BankAccount;
import com.moyq5.mvc.maker.test.bean.search.BankAccount4Search;
import com.moyq5.mvc.maker.test.feign.server.jackson.filter.BankAccountFilter;
import com.moyq5.mvc.maker.test.core.service.BankAccountService;


/**
 * @author Moyq5’s Tools
 * @since 2019-06-17 18:52:23
 */
@RestController
@RequestMapping("/bankAccount")
public class BankAccountController {
	
	private static Logger log = LoggerFactory.getLogger(BankAccountController.class);
	
	@Autowired
	private BankAccountService bankAccountService;
	
	/**
	 * 根据ID获取对象
	 * @author Moyq5’s Tools
	 * @param accountId
	 * @param filter
	 * @return
	 */
	@PostMapping("/oneById")
	@ResponseBody
	public MappingJacksonValue oneById(@RequestBody Long accountId, String filter) {
		log.debug(">>>>oneById: accountId={},filter={}", accountId, filter);
		BankAccount bankAccount = bankAccountService.one(accountId);
		ObjectResult<BankAccount> objRst = new ObjectResult<BankAccount>();
		objRst.setData(bankAccount);
		objRst.setOk();
		MappingJacksonValue value = new MappingJacksonValue(objRst);
		value.setFilters(BankAccountFilter.get(filter));
		return value;
	}
	
	/**
	 * 查询记录数
	 * @author Moyq5’s Tools
	 * @param bankAccount4s
	 * @return
	 */
	@PostMapping("/count")
	@ResponseBody
	public ObjectResult<Long> count(@RequestBody Body<BankAccount4Search, String> bankAccount4s) {
		log.debug(">>>>count: ");
		Long count = bankAccountService.count(bankAccount4s.getEntity(), bankAccount4s.getCustom());
		ObjectResult<Long> objRst = new ObjectResult<Long>();
		objRst.setData(count);
		objRst.setOk();
		return objRst;
	}
	
	/**
	 * 查询单个对象
	 * @author Moyq5’s Tools
	 * @param bankAccount4s
	 * @param filter
	 * @return
	 */
	@PostMapping("/one")
	@ResponseBody
	public MappingJacksonValue one(@RequestBody Body<BankAccount4Search, Sorting> bankAccount4s, String filter) {
		log.debug(">>>>one: filter={}", filter);
		BankAccount bankAccount = bankAccountService.one(bankAccount4s.getEntity(), bankAccount4s.getCustom());
		ObjectResult<BankAccount> objRst = new ObjectResult<BankAccount>();
		objRst.setData(bankAccount);
		objRst.setOk();
		MappingJacksonValue value = new MappingJacksonValue(objRst);
		value.setFilters(BankAccountFilter.get(filter));
		return value;
	}
	
	/**
	 * 列表查询
	 * @author Moyq5’s Tools
	 * @param bankAccount4s
	 * @param filter
	 * @return
	 */
	@PostMapping("/list")
	@ResponseBody
	public MappingJacksonValue list(@RequestBody Body<BankAccount4Search, Sorting> bankAccount4s, String filter) {
		log.debug(">>>>list: filter={}", filter);
		List<BankAccount> list = bankAccountService.list(bankAccount4s.getEntity(), bankAccount4s.getCustom());
		ListResult<BankAccount> listRst = new ListResult<BankAccount>();
		listRst.setData(list);
		listRst.setOk();
		MappingJacksonValue value = new MappingJacksonValue(listRst);
		value.setFilters(BankAccountFilter.get(filter));
		return value;
	}
	
	/**
	 * 分页查询
	 * @author Moyq5’s Tools
	 * @param bankAccount4s
	 * @param filter
	 * @return
	 */
	@PostMapping("/page")
	@ResponseBody
	public MappingJacksonValue page(@RequestBody Body<BankAccount4Search, Paging> bankAccount4s, String filter) {
		log.debug(">>>>page: filter={}", filter);
		Page<BankAccount> page = bankAccountService.page(bankAccount4s.getEntity(), bankAccount4s.getCustom());
		PageResult<BankAccount> pageRst = new PageResult<BankAccount>();
		pageRst.setData(page);
		pageRst.setOk();
		MappingJacksonValue value = new MappingJacksonValue(pageRst);
		value.setFilters(BankAccountFilter.get(filter));
		return value;
	}
	
	/**
	 * 添加
	 * @author Moyq5’s Tools
	 * @param from
	 * @return
	 */
	@PutMapping
	@ResponseBody
	public ObjectResult<Long> create(@RequestBody BankAccount from) {
		log.debug(">>>>add: BankAccount");
		BankAccount bankAccount = bankAccountService.persist(from);
		ObjectResult<Long> result = new ObjectResult<Long>();
		result.setData(bankAccount.getAccountId());
		result.setOk();
		return result;
	}
	
	/**
	 * 更新
	 * @author Moyq5’s Tools
	 * @param form
	 * @return
	 */
	@PatchMapping
	@ResponseBody
	public StatusResult update(@RequestBody BankAccount form) {
		log.debug(">>>>update: BankAccount");
		BankAccount bankAccount = bankAccountService.one(form.getAccountId());
		EntityUtils.combine(form, bankAccount);
		bankAccountService.merge(bankAccount);
		StatusResult result = new StatusResult();
		result.setOk();
		return result;
	}
	
	/**
	 * 删除
	 * @author Moyq5’s Tools
	 * @param accountId
	 * @return
	 */
	@DeleteMapping
	@ResponseBody
	public StatusResult delete(@RequestBody Long accountId) {
		log.debug(">>>>delete: accountId={}", accountId);
		bankAccountService.remove(accountId);
		StatusResult result = new StatusResult();
		result.setOk();
		return result;
	}
	
}
